package cn.yunhe.servlet;

import cn.yunhe.entity.Product;
import cn.yunhe.entity.ProductCategory;
import cn.yunhe.service.ProductCategoryService;
import cn.yunhe.service.ProductService;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
@WebServlet("/updateProductServlet")
public class UpdateProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        SmartUpload smart = new SmartUpload();
        try {
            //PageContext是jsp的内置对象，在servlet不能直接使用，需要做一些处理
            JspFactory _jspxFactory = null;
            PageContext pageContext = null;
            _jspxFactory = JspFactory.getDefaultFactory();
            pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
            smart.setCharset("UTF-8");
            smart.initialize(pageContext);//初始化上传操作
            smart.upload();
            smart.save("images/product");
            //由于前面的form表单已经进行了封装，这里就不能简单的用request.getparameter()来获取表单参数
            String productName = smart.getRequest().getParameter("productName");//用户名
            String picdec = smart.getRequest().getParameter("picdec");//商品描述
            int epid = Integer.parseInt(smart.getRequest().getParameter("epid"));

            //String photoName = smart.getRequest().getParameter("photo");
            Double productPrice = Double.valueOf(smart.getRequest().getParameter("productPrice"));//商品的价格
            int parentId = Integer.parseInt(smart.getRequest().getParameter("parentId"));//商品分类
            int  productNum = Integer.parseInt(smart.getRequest().getParameter("productNum"));

            String picname = smart.getFiles().getFile(0).getFileName();//这里是读取到存储文件的名字
            ProductCategory productCategory = new ProductCategory();
            productCategory.setEpcid(parentId);

            Product product = new Product();
            product.setEpid(epid);
            product.setEpname(productName);
            product.setEpdesc(picdec);
            product.setPrice(productPrice);
            product.setEpstock(productNum);
            product.setEppicturename(picname);
            product.setProductCategory(productCategory);
            ProductService productService = new ProductService();
            boolean b = productService.getProByEpid(product);
            if (b){
                response.sendRedirect("productListServlet");
              //  request.getRequestDispatcher("productListServlet").forward(request,response);
            }


        } catch (SmartUploadException e) {
            e.printStackTrace();
        }



    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
